/**
 * 
 */
package com.dsp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.core.util.Constants;
import com.core.util.User;

/**
 * @author laker
 * 
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController {
	String jsonView;

	/**
	 * @RequestMapping("login") public ModelAndView
	 *                          login(@RequestParam("username") String username,
	 * @RequestParam("password") String password,
	 * @RequestParam("code") String code) { String pager = "login";
	 * 
	 *                       if ("admin".equals(username) &&
	 *                       "admin".equals(password)) { pager = "index"; }
	 * 
	 *                       return new ModelAndView(pager); }
	 */

	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody
	User login1(@RequestParam("username") String username,
			@RequestParam("password") String password,
			@RequestParam("code") String code, Model model) {
		User user = new User();
		user.setUserName(username);
		user.setPassword(password);
		// h:240 s:100 b:29 l:4 a:24 b:-44 61 34
		user.setCode(code);
		if ("admin".equals(username) && "admin".equals(password)) {
			user.setSuccess(true);
			user.setGoToPath("index.jsp");
			//session存放路径
			model.addAttribute(Constants.USER_INFO_SESSION, user);
		} else {
			user.setSuccess(false);
			user.setMessage("登录失败,用户名或者密码不对!");
		}
		return user;
	}

	@RequestMapping(params = "method=logout")
	public String logout(HttpServletRequest request,
			HttpServletResponse response) {
		request.getSession().setMaxInactiveInterval(0);
		return "redirect:login.jsp";
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public String getJsonView() {
		return jsonView;
	}

	public void setJsonView(String jsonView) {
		this.jsonView = jsonView;
	}

}
